##################################################################
# Common Help Stuff PVCS
##################################################################

import os
import sys

##################################################################
# readValueFromPrefsFile()
##################################################################
def readValueFromPrefsFile(PVCSPrefsFileName, ValueName):
	import os
	import string
	retval = ""
	foundKey = 0
	dictReturnValues = {
						"default": "",
						"adaptersubdir": "",
						"pvcsdebug": "0",
						"vlogdebug": "0",
						"vcsdebug": "0"
						}
	try:
		try:
			pvcsPrefsFileObject = open(PVCSPrefsFileName, "r")
			aLine = pvcsPrefsFileObject.readline()
			while aLine != "":
				if string.find(string.lower(aLine), string.lower(ValueName)) != -1:
					foundKey = 1
					splittedLine = string.split(aLine)
					if len(splittedLine) >= 2:
						retval = splittedLine[1]
						break
				aLine = pvcsPrefsFileObject.readline()
			pvcsPrefsFileObject.close()
		except IOError:
			#print "@B@rFile %s doesnt exist !" % PVCSPrefsFileName
			pass
		#print "@B@rfoundKey = %d@x@B" % foundKey
		if not foundKey:
			#print "@B@r(Error) Key not found:'%s'@x@B" % ValueName
			dictKeyName = string.lower(ValueName)
			if dictReturnValues.has_key(dictKeyName):
				retval = dictReturnValues[dictKeyName]
			else:
				retval = dictReturnValues["default"]
	except:
		# oops, exception occured
		#print "@B@r oops, unhandled exception exception occured !"
		#import traceback
		#traceback.print_exc()
		pass
	return retval

##################################################################
# writePrefsFile()
##################################################################
def writePrefsFile(PVCSPrefsFileName, model):
	import os
	import string
	try:
		pvcsPrefsFileObject = open(PVCSPrefsFileName, "w")
		# read the preferences values
		# disabled at 22.06.1999 by USI
		#pvcsMasterConfig = model.GetValue("masterCFGFile")
		#pvcsProjectConfig = model.GetValue("projectCFGFile")
		VCSDebug = model.GetValue("VCSDebug")
		PVCSDebug = model.GetValue("PVCSDebug")
		VLOGDebug = model.GetValue("VLOGDebug")
		AdapterSubDir = model.GetValue("AdapterSubDir")
		# write the preferences values
		# disabled at 22.06.1999 by USI
		#if pvcsMasterConfig != "":
		#	pvcsPrefsFileObject.write("[PVCSMasterCfg]\t%s\n" % pvcsMasterConfig)
		#if pvcsProjectConfig != "":
		#	pvcsPrefsFileObject.write("[PVCSProjectCfg]\t%s\n" % pvcsProjectConfig)
		if VCSDebug:
			pvcsPrefsFileObject.write("[VCSDebug]\t%s\n" % "1")
		else:
			pvcsPrefsFileObject.write("[VCSDebug]\t%s\n" % "0")
		if PVCSDebug:
			pvcsPrefsFileObject.write("[PVCSDebug]\t%s\n" % "1")
		else:
			pvcsPrefsFileObject.write("[PVCSDebug]\t%s\n" % "0")
		if VLOGDebug:
			pvcsPrefsFileObject.write("[VLOGDebug]\t%s\n" % "1")
		else:
			pvcsPrefsFileObject.write("[VLOGDebug]\t%s\n" % "0")
		pvcsPrefsFileObject.write("[AdapterSubDir]\t%s\n" % AdapterSubDir)
		pvcsPrefsFileObject.close()
	except:
		# oops, exception occured
		#print "@B@r oops, unhandled exception exception occured !"
		pass
	return
